﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.FindArtInfo.Entities;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.FindArtInfo.Models;

namespace BusinessLayer.FindArtInfo
{
    public class ArtistsBL
    {
        public List<ArtistsDetailExtend> Search(string keyWord, int? born, int? died, int? country, int pageIndex, int pageSize)
        {
            List<ArtistsDetailExtend> result = new List<ArtistsDetailExtend>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@KeyWord", keyWord);
            adapter.AddParameter("@Born", born);
            adapter.AddParameter("@Died", died);
            adapter.AddParameter("@Country", country);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            try
            {
                result = adapter.GetList<ArtistsDetailExtend>("sp_Artists_Search", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public void GetCountByKeyWord(string keyWord, int? born, int? died, int? country, out int totalRecord)
        {
            totalRecord = 0;            
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@KeyWord", keyWord);
            adapter.AddParameter("@Born", born);
            adapter.AddParameter("@Died", died);
            adapter.AddParameter("@Country", country);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_Artists_Select_Count_By_KeyWord", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = Convert.ToInt32(output.Value);
            }
            catch (Exception)
            {
            }
        }

        public ArtistsDetailExtend GetById(int id)
        {
            ArtistsDetailExtend result = new ArtistsDetailExtend();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@Id", id);            
            try
            {
                result = adapter.GetList<ArtistsDetailExtend>("sp_Artists_Select_By_Id", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit).FirstOrDefault();
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public List<ArtistsDetailExtend> GetByLastNameLetter(string letter, int pageIndex, int pageSize)
        {
            List<ArtistsDetailExtend> result = new List<ArtistsDetailExtend>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);            
            adapter.AddParameter("@Letter", letter);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            try
            {
                result = adapter.GetList<ArtistsDetailExtend>("sp_Artists_Select_By_LastNameLetter", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public void GetCountByLastNameLetter(string letter, out int totalRecord)
        {
            totalRecord = 0;            
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);            
            adapter.AddParameter("@Letter", letter);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_Artists_Select_Count_By_LastNameLetter", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = Convert.ToInt32(output.Value);
            }
            catch (Exception)
            {
            }
        }        
    }
}
